<%--  Sudoku. jsp --%>
<%@page contentType="text/html;charset=UTF-8"%>
<%@ include file='DatabaseSetup.jsp'%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>Web Baesd Intelligent Tutoring System in Sudoku for Logic Training</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <%-- CSS is get from http://www.freelayouts.com/ --%>
    <link rel="stylesheet" type="text/css" href="css/default.css" title="default"/>
    <script src="js/ajax.js"></script>
</head>

<body>
    <% String loginFormURL =
               "http://144.214.121.25:8080/SudokuITSWebsite/login.jsp";
    String user = (String) session.getAttribute("user");
    String langMode = (String) session.getAttribute("langMode");
    
    if(user == null) {
        response.setHeader("Refresh", "3; URL=" + loginFormURL);
    %>


    <jsp:include page='header.jsp'/>
    <div class="article_body">
        <p><h2>
            You have not login and will be redirected to the login page after 3 seconds. If not, press <a href=\"login.jsp\">here</a><br/>
            您還沒登入！3秒後進入登入頁面.如沒有，請按<a href=\"login.jsp\">此</a> <br/>
        </h2></p> 
    </div>
    <jsp:include page='footer.jsp'/>

    <%
    }else{
    %>

    <%--<jsp:include page='header.jsp'/>--%>
<div id="WholePage">
    <div id="Inner">
        <div id="Container">

            <div id="TopPart">
                <div id="Top_right">
                    <div id="Menu">
                        <a href="index.jsp"><span>Home</span></a>
                        <a href="?page=homepage"><span>Sudoku</span></a>
                        <a href="login.jsp" id="login" style="display:none"><span>Register/Login</span></a>
                        <a href="logout.jsp" id="logout" style="display: block"><span>Logout</span></a>
                    </div>
                </div>

                <div id="CentralPart">
                <%--Content Start --%>

                <div class="article_head">
                    <div class="arrow"></div>

                    <%-- Delete any non finished puzzles --%>
                    <%
                            String userID = (String) session.getAttribute("user");

                            String sql="";

                            sql = "Delete from gamesolvingsteps WHERE puzzleID NOT IN " +
                                    " (Select puzzleID from gameskillsmatrix where userID = " + userID +
                                    " ) AND userID = " + userID;
                            stmt.executeUpdate(sql);
                            sql = "Delete from gamestepsmatrix WHERE puzzleID NOT IN " +
                                    " (Select puzzleID from gameskillsmatrix where userID = " + userID +
                                    " ) AND userID = " + userID;
                            stmt.executeUpdate(sql);
                    %>
                    <%-- Auto Hints Control --%>
                    <%--<%
                    String userID = (String) session.getAttribute("user");
                    int min=0;
                    int max=0;
                    String sql="";
                    ResultSet rs=null;
                    String difficulty="";

                    for(int k=1; k<=3; k++){
                        if(k==1){min=30; max=81; difficulty="difficultTimeSlot";}
                        if(k==2){min=25; max=29; difficulty="mediumTimeSlot";}
                        if(k==3){min=0; max=24; difficulty="gentleTimeSlot";}

                        sql="select Count(*) from gamepuzzles where numgivencells between " + min + " and " + max +
                            " and puzzleID in (select puzzleID from gameSkillsMatrix where userID = " +userID +") order by puzzleID";
                         rs = stmt.executeQuery(sql);
                            rs.next();
                            int rowCount = Integer.parseInt(rs.getString(1));
                            int[] puzzleIDs = new int[rowCount];

                        sql="select puzzleID from gamepuzzles where numgivencells between " + min + " and " + max +
                            " and puzzleID in (select puzzleID from gameSkillsMatrix where userID = " +userID +") order by puzzleID";
                        rs = stmt.executeQuery(sql);

                        int j=0;
                        while(rs.next()){
                            puzzleIDs[j]= Integer.parseInt(rs.getString(1));
                            j++;
                        }

                        int []rowHints = new int[puzzleIDs.length];
                        int []colHints = new int[puzzleIDs.length];
                        int []rulesHints = new int[puzzleIDs.length];
                        int []answerHints = new int[puzzleIDs.length];

                        boolean isUpdate =false;
                        for(int i=0; i<puzzleIDs.length; i++){
                            isUpdate =true;
                            sql = "select Count(*) from gamesolvingsteps where userID = "
                                + userID + " and puzzleID = " + puzzleIDs[i] + " and rowHint = '1'";
                            rs = stmt.executeQuery(sql);
                            rs.next();
                            rowHints[i] = Integer.parseInt(rs.getString(1));

                            sql = "select Count(*) from gamesolvingsteps where userID = "
                                + userID + " and puzzleID = " + puzzleIDs[i] + " and colHint = '1'";
                            rs = stmt.executeQuery(sql);
                            rs.next();
                            colHints[i] = Integer.parseInt(rs.getString(1));

                            sql = "select Count(*) from gamesolvingsteps where userID = "
                                + userID + " and puzzleID = " + puzzleIDs[i] + " and rulesHint = '1'";
                            rs = stmt.executeQuery(sql);
                            rs.next();
                            rulesHints[i] = Integer.parseInt(rs.getString(1));

                            sql = "select Count(*) from gamesolvingsteps where userID = "
                                + userID + " and puzzleID = " + puzzleIDs[i] + " and answerHint = '1'";
                            rs = stmt.executeQuery(sql);
                            rs.next();
                            answerHints[i] = Integer.parseInt(rs.getString(1));

                            int totalHints = rowHints[i] + colHints[i] + rulesHints[i] + answerHints[i];
                            if(totalHints>0){
                                isUpdate = false;
                                break;
                            }
                        }
                        if(isUpdate){
                            sql = "select " + difficulty + "from  gameautohintsgivingtime where userID = "+ userID;
                            rs = stmt.executeQuery(sql);
                            rs.next();
                            int newSlot=(Integer.parseInt(rs.getString(1)) -1 );
                            sql = "Update gameautohintsgivingtime Set " + difficulty + " = " + newSlot + " where userID = " + userID;
                            stmt.executeUpdate(sql);
                        }
                    }

                    %>--%>

                    <h2>I want to play the Sudoku in<br/>
                        我想以下的語言完成數獨

                        <select id="lid" onchange="showWebSudoku()">
                            <option value="2"></option>
                            <option value="1">English</option>
                            <option value="0">中文</option>
                        </select>
                    </h2>
                </div>

                <div class="article_body">
                    <p id="result"></p>
                </div>


                <%--Content end --%>
                <%-- for cleaning the black area --%>
                <div class="cleaner"></div>
                
                </div>

                <div id="Bottom">
                    <p class="down_left">Copyright &copy 2009</p>
                </div>
            </div>
        </div>
    </div>
</div>
    <%
        }
    %>
<%--<jsp:include page='footer.jsp'/>--%>

</body>
</html>

